package com.ts.tsavplayer

import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.ts.tsavplayer.databinding.ActivityMainBinding


class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private  val TAG = "MainActivity"
    @RequiresApi(Build.VERSION_CODES.R)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.nativeWindows.setOnClickListener {
           startActivity(Intent(this,NativeWindowsActivity::class.java))
        }

        XXPermissions.with(this)
            // 申请单个权限
            .permission(Permission.RECORD_AUDIO)
            .permission(Permission.READ_MEDIA_IMAGES)
            .permission(Permission.READ_MEDIA_VIDEO)
            .permission(Permission.READ_MEDIA_AUDIO)
            // 申请多个权限

            // 设置权限请求拦截器（局部设置）
            //.interceptor(new PermissionInterceptor())
            // 设置不触发错误检测机制（局部设置）
            //.unchecked()
            .request(object : OnPermissionCallback {

                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    if (!allGranted) {
//                        toast("获取部分权限成功，但部分权限未正常授予")
                        return
                    }
//                    toast("获取录音和日历权限成功")
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    if (doNotAskAgain) {
//                        toast("被永久拒绝授权，请手动授予录音和日历权限")
                        // 如果是被永久拒绝就跳转到应用权限系统设置页面
                        XXPermissions.startPermissionActivity(applicationContext, permissions)
                    } else {
//                        toast("获取录音和日历权限失败")
                    }
                }
            })

//        XXPermissions.with(this) // 申请单个权限
//            .permission(Permission.RECORD_AUDIO) // 申请多个权限
//            .permission(Permission.Group.CALENDAR) // 设置权限请求拦截器（局部设置）
//            //.interceptor(new PermissionInterceptor())
//            // 设置不触发错误检测机制（局部设置）
//            //.unchecked()
//            .request(object : OnPermissionCallback() {
//                fun onGranted(permissions: List<String?>, allGranted: Boolean) {
//                    if (!allGranted) {
//                        toast("获取部分权限成功，但部分权限未正常授予")
//                        return
//                    }
//                    toast("获取录音和日历权限成功")
//                }
//
//                fun onDenied(permissions: List<String?>, doNotAskAgain: Boolean) {
//                    if (doNotAskAgain) {
//                        toast("被永久拒绝授权，请手动授予录音和日历权限")
//                        // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                        XXPermissions.startPermissionActivity(context, permissions)
//                    } else {
//                        toast("获取录音和日历权限失败")
//                    }
//                }
//            })
//        if (!Environment.isExternalStorageManager()){
//            val intent: Intent = Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)
////          startActivity(intent)
//        }
//        PermissionX.init(this)
//            .permissions(
//                Manifest.permission.WRITE_EXTERNAL_STORAGE)
//            .request { allGranted, grantedList, deniedList ->
//                if (allGranted) {
//                    Toast.makeText(this, "All permissions are granted", Toast.LENGTH_LONG).show()
//                } else {
//                    Log.e(TAG,""+deniedList);
//                    Toast.makeText(this, "These permissions are denied: $deniedList", Toast.LENGTH_LONG).show()
//                }
//            }
//        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
//        intent.setData(Uri.fromParts("package", getPackageName(), null))
//        startActivity(intent)

//        if (ContextCompat.checkSelfPermission(this,Manifest.permission.MANAGE_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED) {
            // 权限未授予，请求权限
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//            ActivityCompat.requestPermissions(this,
//                arrayOf(
//                    Manifest.permission.CAMERA,
//                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
//                    Manifest.permission.MANAGE_EXTERNAL_STORAGE,
//                    Manifest.permission.RECORD_AUDIO,
//                    Manifest.permission.READ_EXTERNAL_STORAGE), 12)
//        }
//        } else {
            // 权限已授予，打开摄像头
//            surfaceView=findViewById(R.id.camera_view) as SurfaceView
//            surfaceView?.holder?.addCallback(this)
//        }
    }



}